/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package freimapgsoc;

import java.awt.Color;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JPanel;

public final class FreimapVisualPanel2 extends JPanel {

    /** Creates new form FreimapVisualPanel2 */
    public FreimapVisualPanel2() {
        initComponents();
    }

     @Override
    public String getName() {
        return "MySQL Information";
    }

     public String getHost() {
        return hostText.getText();
    }

     public String getPort() {
        return portText.getText();
    }

      public String getDatabase() {
        return dbText.getSelectedItem().toString();
    }

       public String getUserName() {
        return userText.getText();
    }

        public String getPassword() {
        return passwordText.getText();
    }

        public boolean getStore(){
            return storeCheck.isSelected();
        }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        connectButton = new javax.swing.JButton();
        userText = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        portText = new javax.swing.JTextField();
        dbText = new javax.swing.JComboBox();
        jLabel7 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        loadDb = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        hostText = new javax.swing.JTextField();
        logLabel = new javax.swing.JLabel();
        passwordText = new javax.swing.JPasswordField();
        jLabel3 = new javax.swing.JLabel();
        storeCheck = new javax.swing.JCheckBox();

        org.openide.awt.Mnemonics.setLocalizedText(connectButton, org.openide.util.NbBundle.getMessage(FreimapVisualPanel2.class, "FreimapVisualPanel2.connectButton.text")); // NOI18N
        connectButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                connectButtonActionPerformed(evt);
            }
        });

        userText.setFont(new java.awt.Font("Lucida Grande", 0, 12));

        jLabel4.setFont(new java.awt.Font("Lucida Grande", 0, 8));
        org.openide.awt.Mnemonics.setLocalizedText(jLabel4, org.openide.util.NbBundle.getMessage(FreimapVisualPanel2.class, "FreimapVisualPanel2.jLabel4.text")); // NOI18N

        portText.setFont(new java.awt.Font("Lucida Grande", 0, 12));
        portText.setText(org.openide.util.NbBundle.getMessage(FreimapVisualPanel2.class, "FreimapVisualPanel2.portText.text")); // NOI18N

        dbText.setFont(new java.awt.Font("Lucida Grande", 0, 12)); // NOI18N

        jLabel7.setFont(new java.awt.Font("Lucida Grande", 0, 10));
        org.openide.awt.Mnemonics.setLocalizedText(jLabel7, org.openide.util.NbBundle.getMessage(FreimapVisualPanel2.class, "FreimapVisualPanel2.jLabel7.text")); // NOI18N

        jButton1.setFont(new java.awt.Font("Lucida Grande", 0, 12));
        org.openide.awt.Mnemonics.setLocalizedText(jButton1, org.openide.util.NbBundle.getMessage(FreimapVisualPanel2.class, "FreimapVisualPanel2.jButton1.text")); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(loadDb, org.openide.util.NbBundle.getMessage(FreimapVisualPanel2.class, "FreimapVisualPanel2.loadDb.text")); // NOI18N
        loadDb.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loadDbActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Lucida Grande", 0, 10));
        org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(FreimapVisualPanel2.class, "FreimapVisualPanel2.jLabel1.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(jLabel6, org.openide.util.NbBundle.getMessage(FreimapVisualPanel2.class, "FreimapVisualPanel2.jLabel6.text")); // NOI18N

        jLabel2.setFont(new java.awt.Font("Lucida Grande", 0, 10));
        org.openide.awt.Mnemonics.setLocalizedText(jLabel2, org.openide.util.NbBundle.getMessage(FreimapVisualPanel2.class, "FreimapVisualPanel2.jLabel2.text")); // NOI18N

        jLabel5.setFont(new java.awt.Font("Lucida Grande", 0, 10));
        org.openide.awt.Mnemonics.setLocalizedText(jLabel5, org.openide.util.NbBundle.getMessage(FreimapVisualPanel2.class, "FreimapVisualPanel2.jLabel5.text")); // NOI18N

        hostText.setFont(new java.awt.Font("Lucida Grande", 0, 12));

        logLabel.setFont(new java.awt.Font("Lucida Grande", 0, 10));
        org.openide.awt.Mnemonics.setLocalizedText(logLabel, org.openide.util.NbBundle.getMessage(FreimapVisualPanel2.class, "FreimapVisualPanel2.logLabel.text")); // NOI18N

        passwordText.setFont(new java.awt.Font("Lucida Grande", 0, 12));

        jLabel3.setFont(new java.awt.Font("Lucida Grande", 1, 13)); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(jLabel3, org.openide.util.NbBundle.getMessage(FreimapVisualPanel2.class, "FreimapVisualPanel2.jLabel3.text")); // NOI18N

        storeCheck.setFont(new java.awt.Font("Lucida Grande", 0, 10)); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(storeCheck, org.openide.util.NbBundle.getMessage(FreimapVisualPanel2.class, "FreimapVisualPanel2.storeCheck.text")); // NOI18N

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(22, 22, 22)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(logLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 311, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(layout.createSequentialGroup()
                                .add(jLabel7)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(dbText, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 197, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                            .add(layout.createSequentialGroup()
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                                        .add(jLabel4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 189, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                        .add(layout.createSequentialGroup()
                                            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                                                .add(jLabel5, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .add(jLabel1))
                                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                                                .add(hostText)
                                                .add(org.jdesktop.layout.GroupLayout.LEADING, userText)
                                                .add(org.jdesktop.layout.GroupLayout.LEADING, passwordText, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 170, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                            .add(jLabel6)))
                                    .add(jLabel2))
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(jButton1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 117, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                    .add(portText, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 53, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))))
                    .add(layout.createSequentialGroup()
                        .addContainerGap()
                        .add(loadDb, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 166, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(connectButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 106, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(storeCheck)))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap(40, Short.MAX_VALUE)
                .add(jLabel3)
                .add(62, 62, 62))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(9, 9, 9)
                .add(jLabel3)
                .add(18, 18, 18)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(hostText, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel5)
                    .add(jLabel6)
                    .add(portText, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(userText, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel2)
                    .add(passwordText, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jButton1))
                .add(2, 2, 2)
                .add(jLabel4)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel7)
                    .add(dbText, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(logLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 20, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(18, 18, 18)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(loadDb, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 29, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(connectButton)
                    .add(storeCheck))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    public void findDatabase(String host, String port, String username, String password, String database) throws SQLException, InterruptedException {
        try {
            logLabel.setText("Getting connection...");
            Thread.sleep(1000);
            Class.forName("com.mysql.jdbc.Driver");
            c = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database, username, password);
            if (!c.isClosed()) {
                logLabel.setForeground(Color.green);
                logLabel.setText("Connected!");
            }
        } catch (Exception ex) {
            logLabel.setForeground(Color.red);
            logLabel.setText(ex.getMessage());
            System.out.println(ex.getMessage());
            System.out.println(ex.getCause());
        }
    }

    private void connectButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_connectButtonActionPerformed
        host = hostText.getText();
        port = portText.getText();
        username = userText.getText();
        password = passwordText.getText();
        if (dbText.getSelectedItem().equals("")) {
            logLabel.setForeground(Color.red);
            logLabel.setText("Select a valid Database!");
        } else {
            database = dbText.getSelectedItem().toString();
        }
        if (host.length() < 2) {
            if (port.length() == 0) {
                port = "3306";
            }
            logLabel.setForeground(Color.red);
            logLabel.setText("Add host address and port number!");
        } else {
            try {
                findDatabase(host, port, username, password, database);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }//GEN-LAST:event_connectButtonActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        userText.setText("root");
        passwordText.setText("CiscoSte5785");
        hostText.setText("127.0.0.1");
}//GEN-LAST:event_jButton1ActionPerformed

    private void loadDbActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loadDbActionPerformed
        try {
            host = hostText.getText();
            port = portText.getText();
            username = userText.getText();
            password = passwordText.getText();
            if (host.length() < 2) {
                if (port.length() == 0) {
                    port = "3306";
                }
                logLabel.setForeground(Color.red);
                logLabel.setText("Add host address and port number!");
            } else {
                Class.forName("com.mysql.jdbc.Driver");
                c = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port, username, password);
                if (!c.isClosed()) {
                    stmt = c.createStatement();
                    rss = stmt.executeQuery("SHOW DATABASES");
                    int i = 0;
                    while (rss.next()) {
                        dbText.insertItemAt(rss.getString("Database"), i);
                        i++;
                        dbText.setSelectedIndex(0);
                    }
                }
            }
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
            logLabel.setForeground(Color.red);
            logLabel.setText(ex.getMessage());
        }
}//GEN-LAST:event_loadDbActionPerformed
    private String host,port,username,password,database;
    private Statement stmt;
    private ResultSet rss;
    private Connection c;

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton connectButton;
    private javax.swing.JComboBox dbText;
    private javax.swing.JTextField hostText;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JButton loadDb;
    private javax.swing.JLabel logLabel;
    private javax.swing.JPasswordField passwordText;
    private javax.swing.JTextField portText;
    private javax.swing.JCheckBox storeCheck;
    private javax.swing.JTextField userText;
    // End of variables declaration//GEN-END:variables
}

